package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class ItemPeer extends BasePeer{
	public static final String tableName = "Item";
	public static final String ITEMID = "itemId";
	public static final String TYPE = "type";
	public static final String FKID = "fkId";
	public static final String USERID = "userId";
	public static final String TITLE = "title";
	public static final String POSTTIME = "postTime";
	public static final String DESCRIPTION = "description";
	public static final String FREQUENTFOCUS = "frequentFocus";
	public static final String MOSTCLICK = "mostClick";
	public static final String TYPETAG = "typeTag";
	public static final String[] columnNames=new String[]{"itemId","type","fkId","userId","title","postTime","description","frequentFocus","mostClick","typeTag"};
	public static Item getItemByPKs(Object itemId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ItemPeer.tableName,ItemPeer.columnNames," TRUE  AND `itemId`=? ",new Object[]{itemId,},0, 1,conn);
			Item item = new Item();
		fill(rs,item);
		return item;
	}
	public static Item getItemReferenceColumnsOnlyByPKs(Object itemId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ItemPeer.tableName,new String[]{"itemId",}," TRUE AND `itemId`=?",new Object[]{itemId,}, 0,  1,conn);
			Item item = new Item();
		fill(rs,item);
		return item;
	}
	public static Item getItemByItemId(long itemId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ItemPeer.tableName,ItemPeer.columnNames,"`itemId`=?",new Object[]{itemId},0, 1,conn);
			Item item = new Item();
		fill(rs,item);
		return item;
	}
	public static void fill(ResultSet rs, Item item) throws SQLException {
		if(rs.next()){
			try{item.setItemId(rs.getLong("itemId"));}catch(Exception ex){}
			try{item.setType(rs.getLong("type"));}catch(Exception ex){}
			try{item.setFkId(rs.getLong("fkId"));}catch(Exception ex){}
			try{item.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{item.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{item.setPostTime(rs.getTimestamp("postTime"));}catch(Exception ex){}
			try{item.setDescription(rs.getString("description"));}catch(Exception ex){}
			try{item.setFrequentFocus(rs.getTimestamp("frequentFocus"));}catch(Exception ex){}
			try{item.setMostClick(rs.getLong("mostClick"));}catch(Exception ex){}
			try{item.setTypeTag(rs.getString("typeTag"));}catch(Exception ex){}
			item.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<Item> itemCollection) throws SQLException {
		while(rs.next()){
			Item item=new Item();
			try{item.setItemId(rs.getLong("itemId"));}catch(Exception ex){}
			try{item.setType(rs.getLong("type"));}catch(Exception ex){}
			try{item.setFkId(rs.getLong("fkId"));}catch(Exception ex){}
			try{item.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{item.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{item.setPostTime(rs.getTimestamp("postTime"));}catch(Exception ex){}
			try{item.setDescription(rs.getString("description"));}catch(Exception ex){}
			try{item.setFrequentFocus(rs.getTimestamp("frequentFocus"));}catch(Exception ex){}
			try{item.setMostClick(rs.getLong("mostClick"));}catch(Exception ex){}
			try{item.setTypeTag(rs.getString("typeTag"));}catch(Exception ex){}
			item.resetModified();
			itemCollection.add(item);
		}
	}
	public static boolean update(Item item, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < ItemPeer.columnNames.length; i++) {
			if (((ItemBase)item).modified[i]) {
				columns.add(ItemPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `Item` SET ");
		sql.append("WHERE TRUE AND `itemId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,item.get(column));
		}
		pstmt.setObject(columns.size()+1+0,item.get("itemId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(Item item, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (item.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `Item` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (item.get(column) != null) {
				pstmt.setObject(i + 1 + j, item.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<Item> getItemCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<Item> collection=new Vector<Item>();
		fill(rs,collection);
		return collection;
	}
}
